var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum:1,
        pageSize:10,
        isAll:false,
        invoiceList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this 
        that.getNoFillHistoryList()
    },

    getNoFillHistoryList() {
        var that = this
        app.executeRequest(function (header) {
          wx.request({
            url: app.BASE_URL + '/invoice/fillHistoryList',
            header,
            data: {
              pageNum: that.data.pageNum,
              pageSize:that.data.pageSize
            },
            success: (res) => {
    
              var data = res.data.data
              console.log('------------->',res)

              if ((data.length>0 && data.length<10) || data.length==0) {
                that.setData({
                    isAll:true
                })
              }
              that.setData({
                invoiceList:that.data.invoiceList.concat(data)
              })
            }
          })
    
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    loadMore() {
        if(!this.data.isAll) {
            this.setData({
                pageNum:this.data.pageNum+1
            })
            this.getNoFillHistoryList()
        }
    },

    downLoad(e){
        console.log(e)
        let invoiceOrderCode = e.currentTarget.dataset.code
        wx.showLoading({
          title: '加载中',
        })
        app.executeRequest(function (header) {
            wx.downloadFile({
              url: app.BASE_URL + '/invoice/downloadInvoice?inOrderCode='+invoiceOrderCode,
              header,
              success: function (resinfo) {
                  console.log('resinfo----------->',resinfo)
                  wx.hideLoading()
                  console.log("pdf协议文件已下载")
                  let path = resinfo.tempFilePath;
                  const fs = wx.getFileSystemManager()
                  let fileName = path.split('/')[path.split('/').length-1]
                  fs.saveFile({
                      tempFilePath:path,
                      filePath:wx.env.USER_DATA_PATH+'/'+fileName+'pdf',
                      success:function(res) {
                          console.log('newFilePath--------->',res)
                          let newPath = res.savedFilePath
                          wx.setStorage({
                            key:invoiceOrderCode,
                            data:newPath
                          })
                          console.log(newPath, resinfo)
                          wx.openDocument({
                              filePath: newPath,
                              fileType: 'pdf',
                              showMenu:true,
                              success: function (rest) {
                                  console.log('打开文件成功')
                                  console.log(rest);
                              },
                              fail: function (error) {
                                  wx.showToast({
                                      icon: 'none',
                                      title: '打开文件失败'
                                  });
                              },
                          })
                      },
                      fail:function(res) {
                          console.log('errFailPath---------->',res)
                      }
                  })
                  
              },
              fail: function (err) {
                  console.log('fail')
                  console.log(err)
                  wx.showToast({
                    icon: 'none',
                    title: JSON.stringify(err)
                });
              }
            })
      
          })
    },

    


    showMessage(e) {
        console.log(e)
        let invoiceOrderCode = e.currentTarget.dataset.code
        this.shareInvoiceFile(invoiceOrderCode)
    },
    shareInvoiceFile(orderCode) {
        const that = this
        wx.getStorage({
            key:orderCode,
            success (res) {
                const path = res.data
                wx.shareFileMessage({
                    filePath: path,
                    success() {},
                    fail: console.error,
                })
            },
            fail (err) {
                that.downLoadInvoiceFile(orderCode)
            }
        })
    },
    downLoadInvoiceFile(invoiceOrderCode) {
        wx.showLoading({
          title: '加载中',
        })
        app.executeRequest(function (header) {
            wx.downloadFile({
              url: app.BASE_URL + '/invoice/downloadInvoice?inOrderCode='+invoiceOrderCode,
              header,
              success: function (resinfo) {
                  wx.hideLoading()
                  console.log("pdf协议文件已下载")
                  let path = resinfo.tempFilePath;
                  console.log(path, resinfo)
                  const fs = wx.getFileSystemManager()
                  let fileName = path.split('/')[path.split('/').length-1]
                  fs.saveFile({
                      tempFilePath:path,
                      filePath:wx.env.USER_DATA_PATH+'/'+fileName+'pdf',
                      success:function(res) {
                          console.log('newFilePath--------->',res)
                          let newPath = res.savedFilePath
                          wx.setStorage({
                            key:invoiceOrderCode,
                            data:newPath
                          })
                          console.log(newPath, resinfo)
                          wx.shareFileMessage({
                            filePath: newPath,
                            success() {},
                            fail: console.error,
                         })
                      },
                      fail:function(res) {
                          console.log('errFailPath---------->',res)
                      }
                  })
                  wx.setStorage({
                    key:invoiceOrderCode,
                    data:path
                  })
                  
              },
              fail: function (err) {
                  console.log('fail')
                  console.log(err)
                  wx.showToast({
                    icon: 'none',
                    title: '下载文件失败'
                });
              }
            })
      
          })
    },
    invoiceDetail(e) {
        console.log(e)
        let invoiceOrderCode = e.currentTarget.dataset.code
        if (e.currentTarget.dataset.state=='已开票') {
            wx.navigateTo({
                url: '../invoiceDetail/invoiceDetail?inOrderCode='+invoiceOrderCode,
            })
        }
    }
})